<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
define('ACCESS_START_TIME', $_SERVER['REQUEST_TIME_FLOAT']);
// 定义常量
define('DS', DIRECTORY_SEPARATOR);
define('GZ_PATH', __DIR__);
define('EC_PATH', dirname(GZ_PATH));

require GZ_PATH.DS.'library'.DS.'function.php';
spl_autoload_register('gz_autoload');
// 接收参数
GZ_Api::init();
$url = trim(_GET('url'), '/');

require GZ_PATH.DS.'library'.DS.'access_config.php';

$file = GZ_PATH.DS.'controller'.DS.$url.'.php';

if (file_exists($file)) {
    define('IN_ECS', true);
    define('INIT_NO_USERS', true);
    require EC_PATH.DS.'includes'.DS.'init.php';
    define('IP', real_ip());
    require GZ_PATH.DS.'library'.DS.'session.php';
    if ($sid = _POST('sid'))
    {
        session_id($sid);
        session_start();
        define('SESS_ID', $sid);
    }
    session('discount') || session('discount', 1);
    isset($access[$url]) && $access[$url] && !session('login') && abort(2);
    file_put_contents(date('Y_m_d').'.log', var_export([
        'time' => date('H:i:s'),
        'params' => [
            'get' => $_GET,
            'post' => $_POST,
            'php' => file_get_contents('php://input')
        ]
    ], true), FILE_APPEND);
    $result = require $file;
    file_put_contents(date('Y_m_d').'.log', var_export([
        'result' => $result,
    ], true), FILE_APPEND);
    out($result);
} else {
	echo <<< ___END___

    <!DOCTYPE html>
<html class="no-js">
    <head>
        <title>ECMobile</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body style="padding: 10px;">

        <!--[if lt IE 8]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

        <!-- Add your site or application content here -->

        <p style="font-size: 14px;">感谢使用ECMobile产品</p>
        <p style="font-size: 14px;">官方论坛：<a href="http://bbs.ecmobile.cn/">http://bbs.ecmobile.cn/</a></p>
        <p style="font-size: 14px;">QQ群1：329673575</p>
        <p style="font-size: 14px;">QQ群2：239571314</p>
        <p style="font-size: 14px;">QQ群3：347624547</p>
        <br/>
        <p style="font-size: 14px;">接下来，您可以：</p>
        <p style="font-size: 14px;">API测试：<a target="_blank" href="./test">点击这里</a></p>
        <p style="font-size: 14px;">API文档：<a target="_blank" href="./test/ecmobile.json">点击这里</a></p>

    </body>
</html>

___END___;

}
